package com.itheima.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.common.BaseContext;
import com.itheima.common.R;
import com.itheima.pojo.ShoppingCart;
import com.itheima.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/shoppingCart")
public class ShoppingCartController {
    @Autowired
    private ShoppingCartService shoppingCartService;
    /**
     * 添加购物车
     *
     * @param shoppingCart
     * @return
     */
    @PostMapping("/add")
    public R save(@RequestBody ShoppingCart shoppingCart) {
        // 获取用户id，设置用户id
//        Long  userId = (Long) session.getAttribute("user");
        Long currentId = (Long) BaseContext.getCurrentId();
        shoppingCart.setUserId(currentId);
        /* 菜品或者套餐是否点过
           如何知道烧鹅被点过了？？ 根据烧鹅的菜品id，查询下数据库中有没有烧鹅这个数据
           如何是菜品：select * from shopping_cart where dish_id = ? and user_id = ?
           如果是套餐：select * from shopping_cart where setmeal_id = ? and user_id = ?
        */
        // 判断当前用户点的是菜品还是套餐
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId,currentId);
        //菜品id
        Long dishId = shoppingCart.getDishId();
        if (dishId!=null){//菜品不为空，用户点的一定是菜品
            queryWrapper.eq(ShoppingCart::getDishId,dishId);
        }else {
            queryWrapper.eq(ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
        }
        ShoppingCart one = shoppingCartService.getOne(queryWrapper);
        // 第一次点的时候是新增 ，后面在点击的时候是修改
//        if (one==null){
//            //如果没有，则是新增
//            //设置number=1
//            shoppingCart.setNumber(1);
//            shoppingCart.setCreateTime(LocalDateTime.now());
//            shoppingCartService.save(shoppingCart);
//        }else {
//            //如果有，则是更新
//            one.setNumber(one.getNumber()+1);
//            shoppingCartService.updateById(one);
//        }
        if (one!=null){
            //如果有，则是更新
            one.setNumber(one.getNumber()+1);
            shoppingCartService.updateById(one);
        }else {
            //如果没有，则是新增
            //设置number=1
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCartService.save(shoppingCart);
            one = shoppingCart;
        }
        return R.success(one);
    }

    /**
     * 查询购物车
     *
     * @param
     * @return
     */
    @GetMapping("/list")
    public R<List<ShoppingCart>> getShoppingCart() {
        log.info("修改信息");
        LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ShoppingCart::getUserId,BaseContext.getCurrentId());
        wrapper.orderByAsc(ShoppingCart::getCreateTime);
        List<ShoppingCart> list = shoppingCartService.list(wrapper);
        return R.success(list);
    }

    /**
     * 取消购物车
     *
     * @param shoppingCart
     * @return
     */
    @PostMapping("/sub")
    public R<ShoppingCart> cancel(@RequestBody ShoppingCart shoppingCart) {
        //菜品或者套餐id 查询出当前ShoppingCart数据
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId,BaseContext.getCurrentId());
        //获取菜品id
        Long dishId = shoppingCart.getDishId();
        if(dishId!=null){
            // 如何是菜品：select * from shopping_cart where dish_id = ? and user_id = ?
            queryWrapper.eq(ShoppingCart::getDishId,dishId);
        }else {
            // 如果是套餐：select * from shopping_cart where setmeal_id = ? and user_id = ?
            queryWrapper.eq(ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
        }
        ShoppingCart one = shoppingCartService.getOne(queryWrapper);
        // number -1 然后操作：
        Integer number = one.getNumber();
        number--;
        one.setNumber(number);
        if (number>0){
            // >0  update更新 update set number=? where id = ?
            shoppingCartService.updateById(one);
        }else {
            // <= 0  如何操作？ delete from shopping_cart where dish_id=? and user_id = ?
            shoppingCartService.remove(queryWrapper);
        }
        return R.success(one);
    }

    /**
     * 清空购物车
     *
     * @param
     * @return
     */
    @DeleteMapping("/clean")
    public R<String> delete() {
        // 根据当前用户id进行删除：delete from shoping_cart where user_id = ?
        LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ShoppingCart::getUserId,BaseContext.getCurrentId());
        shoppingCartService.remove(wrapper);
        return R.success("已清空");
    }
}
